package com.xiaoji.authface.service;


import com.xiaoji.authface.repository.User;
import com.xiaoji.authface.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service  // 接口和实现处都需要添加@Service注解
public class UserService implements IUserService {

    /**
     *  @ Autowired
     *  this means to get the bean called userRepository
     *  which is auto-generated by Spring, we will use it to handle the data
     */
    @Autowired  // Field injection is not recommended
    private UserRepository userRepository;

    // 注册用户
    @Override
    public User register(String name, String password, String faceBase64){

        User user = new User();
        user.setUserName(name);
        user.setUserPasswd(password);
        user.setUserPhoto(faceBase64);
        userRepository.save(user);
        return user;
    }

    // 账号登录
    @Override
    public String accountLogin(String name, String password) {
        List<User> users = userRepository.findUsersByUserNameAndUserPasswd(name, password);
        if(!users.isEmpty()){
            return  users.toString();
        }else{
            return "FAILED";
        }
    }
}
